package cn.edu.cuit.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <h1>转发控制器</h1>
 * <li> 1.请求时，生成相应的session，会触发相应的监听【会话监听】
 * <li> 2.转发前，进行了session域值的给定，会触发相应的监听【属性监听】
 * <li> 3.它使用的是注解配置， 不用在web.xml文件中给出节点来配置
 * 
 */
@WebServlet("/ForwardServlet")
public class ForwardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForwardServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 直接往myForward.jsp页转发，但将被全过滤器拦截
		// 因为有转发，请求控制器后，控制器又请求了一个jsp页面【这是在服务器上的动作，客户端不知道】
		String myForward = "ForwardServlet工作...";
		// 转发之前，设置了session域的对象，装进去的就是上面的串
		request.getSession().setAttribute("Forward",myForward);		
		// 使用派发器实现转发
		request.getRequestDispatcher("/myForward.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
